<template>
  <div>
    <el-dialog
      title="修改页面"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="100%"
      @close="close"
    >

      <div>

        <el-table v-loading.fullscreen.lock="listLoading" :data="lpnodes" border fit highlight-current-row style="width: 100%">
          <el-table-column align="center" label="名称" prop="name" sortable="custom" />
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="top" inline class="demo-table-expand">
                <el-form-item label="xpath">
                  <span>{{ props.row.xpath }}</span>
                </el-form-item>
                <el-form-item label="selector">
                  <span>{{ props.row.selector }}</span>
                </el-form-item>
                <el-form-item label="content">
                  <span>{{ props.row.content }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column align="center" label="替换内容" prop="content" sortable="custom">
            <template slot-scope="scope">
              <el-input v-model="scope.row.replace_content" type="textarea" :rows="8" class="edit-input" size="small" />
              <el-button size="small" type="primary" @click="handleSaveEdit(scope.row)">保存
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button @click="innerDialogVisible=true">查看</el-button>

      </div>
    </el-dialog>
    <el-dialog
      title="填入替换内容"
      :visible.sync="editDialogVisible"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-input
        v-model="editTemp.code"
        type="textarea"
        :autosize="{ minRows: 6, maxRows: 20}"
        placeholder="请输入内容"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleRepalceCode">确 定</el-button>
        <el-button @click="editDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="innerDialogVisible"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <iframe style="width: 100%; min-height: 600px" :src="view_url" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="innerDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete, LPReplace } from '@/api/landingpage/lppage'
import { Get as GetLPNode } from '@/api/landingpage/lpnode'
import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Lppageeditor',
  props: ['lppage_id', 'showDialog'],
  data() {
    return {
      editTemp: {
        lppage_id: '',
        lpnode_id: '',
        code: ''
      },
      editDialogVisible: false,
      view_url: '',
      data: [],
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      landingpage_id: '',
      action: null,
      dialogVisible: false,
      title: '修改节点',
      temp: {
        landingpage_id: '',
        name: '',
        selector: '',
        xpath: '',
        content: ''
      },
      lpnodes: [],
      width: '',
      base_view_url: '',
      innerDialogVisible: false
    }
  },
  watch: {
    lppage_id(v) {
      if (v) {
        this.getList()
      }
    },
    showDialog(v) {
      this.dialogVisible = v
    }
  },
  methods: {
    handleSaveEdit(row) {
      this.editTemp.lppage_id = this.lppage_id
      this.editTemp.lpnode_id = row.id
      this.editTemp.replace_content = row.replace_content
      this.handleAction('edit', row)
      this.confirm()
    },
    handleModify(row) {
      this.editTemp.lppage_id = this.lppage_id
      this.editTemp.lpnode_id = row.id
      this.editDialogVisible = true
    },
    handleRepalceCode() {
      LPReplace(this.editTemp).then(response => {
        this.editDialogVisible = false
        this.getList()
      })
    },
    handleGetLPNode() {
      this.listLoading = true
      GetLPNode(null, qs.stringify({
        kw: this.kwInput,
        landingpage_id: this.landingpage_id,
        pagesize: 1000,
        page: 1,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.lpnodes = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(e => {
          this.listLoading = false
        })
    },
    getList() {
      this.listLoading = true
      console.log('star to get data')
      Get(this.lppage_id)
        .then(response => {
          this.data = response.data
          if (this.data !== undefined) {
            this.landingpage_id = this.data.landingpage_id
            this.view_url = this.base_view_url + 'token=' + this.data.token
            console.log('change token:' + this.view_url)
            this.handleGetLPNode()
          }
          this.listLoading = false
          this.total = response.data.total
        }).catch(e => {
          this.listLoading = false
        })
    },
    handleAction(action, row) {
      this.action = action
      this.temp.landingpage_id = this.landingpage_id
      switch (action) {
        case 'create':
          this.innerDialogVisible = true
          this.title = row
          break
        case 'edit':
          this.temp.id = row.id
          this.temp.landingpage_id = row.landingpage_id
          this.temp.name = row.name
          this.temp.xpath = row.xpath
          this.temp.content = row.content
          this.title = '修改' + row.name
          this.innerDialogVisible = true
          break
        case 'delete':
          this.dialogVisible = true
          this.title = `确认要删除${row.wpaccount_domain}模板${row.name}?`
          this.temp = row
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          Post(_data).then(response => {
            this.dialogVisible = false
            this.getList()
            this.notify('添加成功')
          })
          return
        case 'edit':
          Put(_data.id, {
            name: _data.name,
            xpath: _data.xpath,
            selector: _data.selector,
            content: _data.content
          }).then(response => {
            this.dialogVisible = false
            this.getList()
            this.notify('更新成功')
          })
          return
        case 'delete':
          Delete(_data.id).then((response) => {
            this.dialogVisible = false
            this.getList()
            this.notify('删除成功')
          })
          return
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.searchModule()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.handleSearch()
    },
    // 点击搜索
    handleSearch() {
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.handleSearch()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    created() {
      this.base_view_url = process.env.VUE_APP_BASE_API + '/cms/api/view_html/?'
    },
    close() {
      this.$emit('closeMyDialog', false)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin-top: 60px;
    /*margin: 168px auto !important;*/
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
  }
</style>
